import threading


class QueryableLock:
    def __init__(self):
        self._lock = threading.Lock()
        self._is_locked = False
    
    def acquire(self):
        with self._lock:
            if not self._is_locked:
                self._is_locked = True
                return True
        return False
    
    def release(self):
        with self._lock:
            self._is_locked = False
    @property
    def is_locked(self):
        with self._lock:
            return self._is_locked
        
    def __enter__(self):
        """with语句入口"""
        self.acquire()
        return self  # 通常返回锁自身或资源句柄
    
    def __exit__(self, exc_type, exc_val, exc_tb):
        """with语句退出时自动释放锁"""
        self.release()
